<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\analysis\data\material\pic;

use app\model\mongo\MongoMaterialPicDayStatModel;
use app\model\mongo\MongoMaterialPicMonthStatModel;
use app\model\mongo\MongoStatModel;
use app\service\analysis\data\material\stat\MonthStatTrait;
use app\service\BaseService;
use think\Model;

final class PicMonthStatService extends BaseService
{
    use MonthStatTrait;
    public function __construct(MongoStatModel $log, int $taskType = 0)
    {
        $this->log = $log;
        $this->taskType = $taskType;
    }

    /**
     * 获取月汇总数据.
     * @return array|mixed|Model|MongoMaterialPicMonthStatModel
     */
    protected function getMonthStatModel()
    {
        return (new MongoMaterialPicMonthStatModel())
            ->where([
                ['stat_month', '=', $this->log->getAttr('stat_month')],
                ['signature', '=', $this->log->getAttr('signature')],
                ['area_id', '=', $this->log->getAttr('area_id')],
            ])
            ->findOrEmpty();
    }

    /**
     * 获取月汇总字段值
     * @param array $data 更新数据
     */
    protected function getMonthStatField(array $data = []): array
    {
        // 消耗查询
        $map = [
            ['stat_month', '=', $this->log->getAttr('stat_month')],
            ['signature', '=', $this->log->getAttr('signature')],
            ['area_id', '=', $this->log->getAttr('area_id')],
        ];

        $data['qq']['cost'] = (new MongoMaterialPicDayStatModel())->where($map)->sum('qq.cost');
        $data['oe']['cost'] = (new MongoMaterialPicDayStatModel())->where($map)->sum('oe.cost');
        $data['ks']['cost'] = (new MongoMaterialPicDayStatModel())->where($map)->sum('ks.cost');
        return $data;
    }
}
